var btn = $("button"),
	table = $(".table"),
	card = $(".card"),
	send_num = 0,
	card_top = 300;

btn.on("click", send_card)

function send_card() {
	var new_card = card.clone();
	table.append(new_card);
	if(send_num < 4) {
		send_num++;
		new_card.animate({
			"top": card_top + "px",
			"left": (send_num * 2) + "00px"
		}, 500, "linear", send_card)
	} else if(card_top < 340) {
		send_num = 0;
		card_top += 20;
		send_card();
	} else if(card_top < 380) {
		send_num = 0;
		card_top += 20;
		change_card();
	}

}

function change_card() {
	var random_num = Math.floor(Math.random() * 20)
	var new_card1 = card.clone();
	table.append(new_card1);
	new_card1.append("<img src='img/" + random_num + ".png'/>");
	if(send_num < 4) {
		send_num++;
		new_card1.animate({
			"top": card_top + "px",
			"left": (send_num * 2) + "00px"
		}, 500, "linear", change_card)
		new_card1.addClass("change");

	}
}